import { login } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: JSON.parse(localStorage.getItem('userInfo')) || {}

  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
      setToken(token)
    },
    REMOVE_TOKEN(state) {
      state.token = ''
      removeToken()
    },
    // 获取用户信息赋值
    USER_INFO(state, userInfo) {
      state.userInfo = userInfo
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
      console.log(state.userInfo, '这是拿到的吗？')
    }

  },
  actions: {
    async loginApi(store, loginForm) {
      const res = await login(loginForm)
      store.commit('SET_TOKEN', res.data.token)
      store.commit('USER_INFO', res.data)
      return res
    }
  }
}
